/*
 * $HeadURL:  $
 *
 * Copyright (c) 2007 Drutt Corporation, all rights reserved.
 *
 */
package homework.james.xiao.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class StartServer {
	public static int ORDER_PORT = 1234;
	public static int ADMIN_PORT = 4321;
	private ServerSocket orderSocket;//处理客户请求
	private ServerSocket adminSocket;//管理
    private ExecutorService executorService;//线程池
    private final int POOL_SIZE=10;//单个CPU线程池大小
    
    public StartServer() throws IOException{
        orderSocket=new ServerSocket(ORDER_PORT);
        adminSocket=new ServerSocket(ADMIN_PORT);
        //Runtime的availableProcessor()方法返回当前系统的CPU数目.
        executorService=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
        System.out.println("starting server");
    }

    
	public static void main(String args[]) throws IOException, InterruptedException {
		if (args.length > 0) {
			ORDER_PORT = Integer.valueOf(args[0]);
		}
		StartServer ss = new StartServer();
		new Thread(new OrderServer(ss.orderSocket, ss.executorService)).start();
		new Thread(new AdminServer(ss.adminSocket, ss.executorService)).start();
		while(true){
			if(ss.executorService.isShutdown()){
				ss.orderSocket.close();
				ss.adminSocket.close();
				break;
			}
			Thread.sleep(5000);
		}
	}
	
}
